import { appStore } from '@/sotre/modules/app';
import { post, get } from '@/api/index';

/**
 * 产品判断
 * @param {产品} app
 */
export const permissionApp = app => {
    return new Promise(async (resolve, reject) => {
        const { userInfo, products } = toRefs(appStore());
        if (app.openType === 1) {
            const product = products.value.find(item => item.platformProduct.related === app.id);
            if (product) {
                return resolve(true);
            }

            return uni.showModal({
                title: '当前产品需要邀请码',
                confirmText: '确定',
                cancelText: '取消',
                placeholderText: '请输入邀请码',
                modelValue: '',
                editable: true,
                success: res => {
                    if (res.confirm) {
                        if (!res.content) {
                            uni.showToast({
                                title: '邀请码不能为空',
                                icon: 'none',
                            });
                            return resolve(false);
                        }
                        post(`/platform/redeemCode/redeem/${res.content}`)
                            .then(res => {
                                if (res.code === 200) {
                                    // 获取最新自己的产品数据
                                    get('/platform/platformEnterprise/products').then(res => {
                                        products.value = res.data;
                                    });
                                    resolve(true);
                                } else {
                                    resolve(false);
                                }
                            })
                            .catch(err => {
                                resolve(false);
                            });
                    }
                },
            });
        }
        // 免费直接放开
        if (!app.chargType) {
            return resolve(true);
        }

        // 判断智能体
        if (app.botId) {
            // 判断套餐
            if (!userInfo.value.currEnterpriseByCache.appUserPower) {
                uni.showToast({
                    title: '暂无权限,请联系管理员开通',
                    icon: 'none',
                });
                return resolve(false);
            }
            // 判断权限
            if (userInfo.value.currEnterpriseByCache.appUserPower.length) {
                const power = userInfo.value.currEnterpriseByCache.appUserPower.find(
                    i => i.appid == app.appid
                );
                if (power && power.use) {
                    return resolve(true);
                }
                uni.showToast({
                    title: '暂无使用权限',
                    icon: 'error',
                });
                return resolve(false);
            }
            uni.showToast({
                title: '暂无权限,请联系管理员开通',
                icon: 'none',
            });
            return resolve(false);
        }
        // 判断套餐
        return resolve(false);
    });
};
